package com.hdyg.sixg.activity.mine;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.adapter.AreaAdapter;
import com.hdyg.sixg.app.MvpApplication;
import com.hdyg.sixg.entry.CountryBean;
import com.hdyg.sixg.entry.ImageBean;
import com.hdyg.sixg.entry.RealNameBean;
import com.hdyg.sixg.mvp.Contrant.CRealName;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.presenter.PRealName;
import com.hdyg.sixg.util.FileSizeUtil;
import com.hdyg.sixg.util.ImageLoadUtil;
import com.hdyg.sixg.util.LogUtils;
import com.hdyg.sixg.util.PopWindowUtil;
import com.hdyg.sixg.util.StringUtil;
import com.hdyg.sixg.util.TakePhotoUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.io.File;
import java.util.List;

import butterknife.BindView;

/**
 * @author CZB
 * @describe 实名认证
 * @time 2019/4/22 15:31
 */
public class RealNameActivity extends BaseActivity implements CRealName.IVRealName {

    private final static int ID_CARD_FRONT = 0x000001;  //正面
    private final static int ID_CARD_BACK = 0x000002;   //背面

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.ll_area)
    LinearLayout llArea;
    @BindView(R.id.tv_area)
    TextView tvArea;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_idcard)
    EditText etIdcard;
    @BindView(R.id.iv_idcard_front)
    ImageView ivIdcardFront;
    @BindView(R.id.tv_idcard_front)
    TextView tvIdcardFront;
    @BindView(R.id.iv_idcard_back)
    ImageView ivIdcardBack;
    @BindView(R.id.iv_area)
    ImageView ivArea;
    @BindView(R.id.tv_idcard_back)
    TextView tvIdcardBack;
    @BindView(R.id.bt_sure)
    Button btSure;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private int type;
    private CRealName.IPRealName mPresenter;
    private String country_id, name, card, front_img, back_img;
    private List<CountryBean> areaBeans;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_real_name;
    }

    @Override
    protected void initView() {
        tvTopTitle.setText(R.string.mine_realname);
        onClickListener();
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            initData();
        });
        llTopLeft.setOnClickListener(v -> finish());
        //国家选择
        llArea.setOnClickListener(v -> showAreaPop(v));
        //身份证正面
        ivIdcardFront.setOnClickListener(v -> {
            type = ID_CARD_FRONT;
            selectPhoto();
        });
        //身份证背面
        ivIdcardBack.setOnClickListener(v -> {
            type = ID_CARD_BACK;
            selectPhoto();
        });
        //完成
        btSure.setOnClickListener(v -> {
            name = etName.getText().toString().trim();
            card = etIdcard.getText().toString().trim();
            if (TextUtils.isEmpty(country_id)) {
                toastNotifyShort(R.string.regist_area_select);
                return;
            }
            if (TextUtils.isEmpty(name)) {
                toastNotifyShort(R.string.realname_name_hint);
                return;
            }
            if (TextUtils.isEmpty(card)) {
                toastNotifyShort(R.string.realname_idcard_hint);
                return;
            }
            if (TextUtils.isEmpty(front_img)) {
                toastNotifyShort(R.string.toast_send_idcard_front);
                return;
            }
            if (TextUtils.isEmpty(back_img)) {
                toastNotifyShort(R.string.toast_send_idcard_back);
                return;
            }
            if (StringUtil.isFastDoubleClick()) {
                return;
            }
            mPresenter.pGetSubmitInfo(BaseUrl.SUBMIT_INFO_URL, GetParamUtil.getSubmitInfoParam(country_id, name, card, front_img, back_img));
        });
    }


    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.REAL_NAME_URL, GetParamUtil.getRealNameParam());
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PRealName(this);
    }

    //国家选择弹窗
    private void showAreaPop(View v) {
        if (areaBeans == null) {
            toastNotifyShort(R.string.toast_area);
            return;
        }
        View view = View.inflate(mContext, R.layout.pop_area, null);
        RecyclerView rvArea = view.findViewById(R.id.rv_area);
        rvArea.setLayoutManager(new LinearLayoutManager(mContext));
        AreaAdapter areaAdapter = new AreaAdapter(R.layout.item_area, areaBeans);
        rvArea.setAdapter(areaAdapter);
        areaAdapter.setOnItemClickListener((adapter, view1, position) -> {
            country_id = areaBeans.get(position).getId();
            tvArea.setText(areaBeans.get(position).getCountry());
            tvArea.setTextColor(getResources().getColor(R.color.white));
            mPopupWindow.dismiss();
        });
        mPopupWindow = PopWindowUtil.getInstance().makePopupWindow(mContext, view, ViewGroup.LayoutParams.MATCH_PARENT)
                .showAsDropDown(mContext, v, 0);

    }

    //上传照片方法
    private void selectPhoto() {
        TakePhotoUtil.getInstance().selectPhotoCropSingle(mContext, obj -> {
            double imgSize = FileSizeUtil.getFileOrFilesSize(obj.toString(), FileSizeUtil.SIZETYPE_MB);//图片大小
            LogUtils.e("图片大小==>" + imgSize);
            //显示图片小于2M才能上传
            if (imgSize <= BaseUrl.IMG_SIZE_LIMIT) {
                File file = new File(obj.toString());
                mPresenter.pGetUploadImg(BaseUrl.UPLOAD_URL, file, "picture", GetParamUtil.getUploadImageParam());
            } else {
                String str = String.format(getResources().getString(R.string.toast_image_size), String.valueOf(BaseUrl.IMG_SIZE_LIMIT));
                toastNotifyShort(str);
            }
        });
    }

    //设置是否可编辑 true可编辑 false不可编辑
    private void setEditUI(boolean flag) {
        etName.setFocusableInTouchMode(flag);
        etName.setFocusable(flag);
        etIdcard.setFocusableInTouchMode(flag);
        etIdcard.setFocusable(flag);
        ivIdcardFront.setClickable(flag);
        ivIdcardBack.setClickable(flag);
        llArea.setClickable(flag);
        if (flag) {
            //可编辑
            tvIdcardFront.setVisibility(View.VISIBLE);
            tvIdcardBack.setVisibility(View.VISIBLE);
            btSure.setVisibility(View.VISIBLE);
            ivArea.setVisibility(View.VISIBLE);
        } else {
            //不可编辑
            tvIdcardFront.setVisibility(View.GONE);
            tvIdcardBack.setVisibility(View.GONE);
            btSure.setVisibility(View.INVISIBLE);
            ivArea.setVisibility(View.GONE);
        }
    }

    /**
     * 实名认证初始化
     *
     * @param dataBean 返回实名信息
     */
    @Override
    public void vGetInitSuccess(RealNameBean dataBean) {
        if (1 == dataBean.getHave_realname()) {
            //已实名  不可编辑
            setEditUI(false);
            etName.setText(dataBean.getRealname().getName());
            etIdcard.setText(dataBean.getRealname().getCard());
            tvArea.setText(dataBean.getRealname().getCountry());
            tvArea.setTextColor(getResources().getColor(R.color.white));
            ImageLoadUtil.loadRoundImage(mContext, dataBean.getRealname().getFront_img(), ivIdcardFront);
            ImageLoadUtil.loadRoundImage(mContext, dataBean.getRealname().getBack_img(), ivIdcardBack);
        } else {
            //未实名 可编辑
            areaBeans = dataBean.getAreacode();
            setEditUI(true);
        }
    }

    /**
     * 提交实名信息
     *
     * @param dataBean 返回参数
     */
    @Override
    public void vGetSubmitInfoSuccess(String dataBean) {
        toastNotifyShort(dataBean);
        finish();
    }

    /**
     * 上传图片
     *
     * @param dataBean 返回图片地址
     */
    @Override
    public void vGetUploadImgSuccess(ImageBean dataBean) {
        if (type == ID_CARD_FRONT) {
            //正面
            front_img = dataBean.getSrc();
            ImageLoadUtil.loadRoundImage(mContext, front_img, ivIdcardFront);
        } else if (type == ID_CARD_BACK) {
            //背面
            back_img = dataBean.getSrc();
            ImageLoadUtil.loadRoundImage(mContext, back_img, ivIdcardBack);
        }
    }
}
